package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WorkSpaceMapper workSpaceMapper;

    /**
     * 根据时间段统计营业额数据
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

//    获取订单总数
        Integer totalOrderCount = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);

//        获取营业额
        Double turnover = orderMapper.sumByMap(map);
//        赋值语句,判断turnover是否为null,如果为null则将0.0赋值给turnover,否则保持其原值
        turnover = turnover == null ? 0.0 : turnover;

//        有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);
//        初始化平均客单价
        Double unitPrice = 0.0;
//        初始化订单完成率
        Double orderCompletionRate = 0.0;
// 营业额：当日已完成订单的总金额
        //订单完成率
        if (totalOrderCount != 0 && validOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;

//        平均客单数
            unitPrice = turnover.doubleValue() / validOrderCount;
        }
        //新增用户数
        Integer newUsers = userMapper.countByMap(map);

        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(validOrderCount)
                .unitPrice(unitPrice)
                .build();
        return businessDataVO;
    }

    /**
     * 根据时间段统计订单数据
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderOverViewVO orderOverView(LocalDateTime begin, LocalDateTime end) {
     /*   //待接单数量
        private Integer waitingOrders;
        //待派送数量
        private Integer deliveredOrders;
        //已完成数量
        private Integer completedOrders;
        //已取消数量
        private Integer cancelledOrders;
        //全部订单
        private Integer allOrders;*/

        Map map = new HashMap();
        map.put("begin", begin);
//        map.put("end",end);
        //待接单数量
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);
        map.put("status", Orders.CONFIRMED);
        //待派送数量
        Integer deliveredOrders = orderMapper.countByMap(map);
        //已完成数量
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        //已取消数量
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);


        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders).build();
        return orderOverViewVO;
    }

    /**
     * 查询菜品启售停售状态
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return dishOverViewVO;
    }

    /**
     * 查询套餐启售停售状态
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }


}
